<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const arr = [
            ["a", "aa", "aaa", "aaaa"],
            ["b", "bb", "bbb"],
            ["a", "ab", "aba"],
            ["a", "aa", "aab"]
        ]
        
        const linkNodes = []
        const tree = []
        arr.forEach(item => {
            const link = toLinkNode(item)
            linkNodes.push(link)
            mergeLinks(tree, link)
        });
        
        function toLinkNode(arr) {
            const list = []
            const head = {
                name: arr[0],
                children: []
            }
            let preNode = head
            arr.slice(1).forEach((item, index)=> {
                const curNode = {
                    name: item,
                    children: []
                }
                preNode.children.push(curNode)                
                preNode = curNode
            })
            return head
        }
        console.log(linkNodes)
        console.log(tree)
        /**
         *  @desc // 二维数组第一项["a", "aa", "aaa", "aaaa"]
            [{ 
                name: 'a', 
                child: [{ name: 'aa', child: [{ name: 'aaa', child: [{name: 'aaaa', child: []}]}]}]
            }]

            // 二维数组第二项 ["b", "bb", "bbb"]
            [{ 
                name: 'b', 
                child: [{ name: 'bb', child: [{ name: 'bbb', child: []}]}]
            }]

            // 二维数组第三项["a", "ab", "aba"]转成链表
            [{ 
                name: 'a', 
                child: [{ name: 'ab', child: [{ name: 'aba', child: []}]}]
            }]

            // 二维数组第四项["a", "aa", "aab"]转成链表
            [{ 
                name: 'a', 
                child: [{ name: 'aa', child: [{ name: 'aab', child: []}]}]
            }]
         * */ 
        function mergeLinks(tree, linkNode) {
            const treeNode = tree.find((item)=> item.name === linkNode.name)
            if(treeNode) {
               return mergeLinks(treeNode.children, linkNode.children[0])
            }
            
            tree.push(linkNode)
        }

    </script>
</body>
</html>